package com.starblink.basic.style.view.mutian


import android.content.Context
import android.util.AttributeSet
import android.util.Log
import com.starblink.basic.style.R
import com.starblink.basic.style.view.SafeImageView
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlin.coroutines.CoroutineContext

/**
 * BorderImageView supports segmented style images, borders, indicators, and initials.
 */
class MTFrameAnimImageView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : SafeImageView(context, attrs, defStyleAttr), CoroutineScope {

    private var animationsContainer: AnimationsContainer2.FramesSequenceAnimation? = null
    private val animationListener: AnimationsContainer2.AnimationListener =
        AnimationsContainer2.AnimationListener {
            animationsContainer?.start()
        }
    private val defaultModels =
        arrayListOf(AnimationResourceModel("frame_anim_empty", "empty_", "frame", true, 149))

    override val coroutineContext: CoroutineContext =
        SupervisorJob() + Dispatchers.Main

    private var defaultModel = true

    init {
        initAttributes(attrs, defStyleAttr)
    }

    private fun initAttributes(attrs: AttributeSet?, defStyleAttr: Int) {
        attrs ?: return
        context.obtainStyledAttributes(attrs, R.styleable.MTFrameAnimImageView, defStyleAttr, 0)
            .let { typedArray ->
                try {
                    defaultModel = typedArray.getBoolean(
                        R.styleable.MTFrameAnimImageView_defaultModel, true
                    )
                } catch (e: Throwable) {
                    e.printStackTrace()
                } finally {
                    typedArray.recycle()
                }
            }
    }

    fun setFrameAnim(models: ArrayList<AnimationResourceModel>) {
        if (animationsContainer == null) {
            animationsContainer =
                AnimationsContainer2.getInstance().createAnimation(
                    context, this, models,
                )
            animationsContainer?.setOnAnimStopListener(animationListener)
            //Log.e("frameAnim --->", "setFrameAnim  Init")
        } else {
            animationsContainer?.reInit(models)
            //Log.e("frameAnim --->", "setFrameAnim  reInit")
        }

        animationsContainer?.start()
    }


    override fun onDetachedFromWindow() {
        //Log.e("frameAnim --->", "onDetachedFromWindow")
        animationsContainer?.realStop()
        coroutineContext.cancel()
        super.onDetachedFromWindow()
    }

    override fun onAttachedToWindow() {
        //Log.e("frameAnim --->", "onAttachedToWindow")
        super.onAttachedToWindow()
        //有内存抖动
//        if (defaultModel) {
//            setFrameAnim(defaultModels)
//        }
    }

    fun changeAnimState(ifShow:Boolean){
        if (ifShow&&defaultModel) {
            setFrameAnim(defaultModels)
        }else{
            animationsContainer?.realStop()
        }
    }
}
